--登陆模块
module("Module.LoginModule", package.seeall)
--欢乐场
LoginModule = {}

local UpdateConfig = Asset.Script.Update.UpdateConfig
LoginModule.Logining = false
LoginModule.SessionUrl = "http://192.168.0.106:8080/qipaiLogin/user!login.action" --覃然

local json = require("cjson")
local NetMgr = require("NetWork").NetMgr
local UIWinMgr = require("UI.UIWinMgr").UIWinMgr


--获取登陆数据
function LoginModule:GetLoginData(_username, _password, callBack)
    if self.LockGetLoginData then
        Debug.log("正在获取登陆数据中")
        return
    end

    self.LockGetLoginData = true
    function LoginDataCallBack(ok, strData)
        self.LockGetLoginData = false
        if not ok then
            UIWinMgr:OpenTextNotice(strData.msg)
            callBack(nil)
            return
        end

        local jsondata = strData


        if 200 == strData.code then
            UnityEngine.PlayerPrefs.SetString("denglu_mima", _password)     --缓存帐号
            UnityEngine.PlayerPrefs.SetString("denglu_zhanghao",_username ) --缓存密码
            
            require("NetWork.NetHttp").NetHttp.token=jsondata.data.access_token
        end

        callBack(jsondata)
    end

    local version = "1.0.1"

    local loginJson = string.format('{"username": "%s", "password": "%s"}', _username,
        _password)


 
    require("NetWork.NetHttp").SendPostRequest("/aut/buyer/login", LoginDataCallBack,
        loginJson)


    Debug.log("开始获取登陆数据")
    Debug.LogErrorEx("self.SessionUrl: " .. tostring(self.SessionUrl))
end

--FaceBook获取登陆数据
function LoginModule:FBGetLoginData(token, ps, userName, userID, callBack)
    if self.LockGetLoginData then
        Debug.log("正在获取登陆数据中")
        return
    end
    self.LockGetLoginData = true
    function FBLoginDataCallBack(ok, strData)
        self.LockGetLoginData = false
        if not ok then
            local str = UIWinMgr:GetIntTextByCode(48)
            UIWinMgr:OpenNotice(str)
            --UIWinMgr:OpenNotice("获取登陆数据失败")
            callBack(nil)
            return
        end

        local jsondata = json.decode(strData)
        Debug.LogErrorEx("3333333333333333333333登陆数据：" .. jsondata.code)

        if 0 == jsondata.code then
            UnityEngine.PlayerPrefs.SetString("lastPs", ps)                          --缓存密码
            UnityEngine.PlayerPrefs.SetString("lastSession", jsondata.user.username) --缓存Session
            UnityEngine.PlayerPrefs.SetString("lastIp", jsondata.user.connectHost)   --缓存IP
            UnityEngine.PlayerPrefs.SetString("lastPort", jsondata.user.port)        --缓存port
            UnityEngine.PlayerPrefs.SetString("curPlatform", jsondata.user.pf)       --缓存当前登陆平台
        end
        callBack(jsondata)
    end

    wwwfrom = UnityEngine.WWWForm()
    local version = "1.0.1"
    --wwwfrom:AddField("u", token)
    --wwwfrom:AddField("ps", 111111)
    wwwfrom:AddField("code", token)
    wwwfrom:AddField("p", "facebook")
    wwwfrom:AddField("openid", userID)
    wwwfrom:AddField("name", userName)
    wwwfrom:AddField("ps", ps)
    local ConfigModule = require("Module.ConfigModule").ConfigModule
    self.SessionUrl = ConfigModule.SessionUrl
    require("NetWork.NetHttp").SendRequest(self.SessionUrl, FBLoginDataCallBack, wwwfrom)
    Debug.log("开始获取登陆数据")
    Debug.LogErrorEx("self.SessionUrl: " .. tostring(self.SessionUrl))
end

--登陆游戏
function LoginModule:Login(uid, sessionCode, loginTime, serverIp, port, isCrossServer)
    self.chgIp = nil
    self.chgPort = nil
    Debug.LogErrorEx("self.LockLoginself.LockLogin----------------------" .. tostring(self.LockLogin))
    if self.LockLogin then
        Debug.log("正在登陆游戏中")
        return
    end
    self.LockLogin = true
    function AsyncCallBack(isOk)
        self.LockLogin = false
        if isOk then
            --uid存下来
            UnityEngine.PlayerPrefs.SetString("LastAccount", uid)
            --session存下来
            UnityEngine.PlayerPrefs.SetString("LastSec", sessionCode)

            local FightOfflineModule = require("Module.FightOfflineModule").FightOfflineModule
            FightOfflineModule.UnRegisterOffLine()
            FightOfflineModule.RegisterOffLine()

            local VersionTool = require("VersionTool").VersionTool
            --秦然新消息
            local Msg = require("Protol.openMsg_pb").Open()
            Msg.userId = tostring(uid)

            Msg.t = tostring(loginTime)
            Debug.LogErrorEx("uid" .. uid .. "--------loginTime----------" .. loginTime)
            Msg.s = LuaHelper.Instance:MyGetMD5(uid .. Msg.t .. "Zehy6f3TDoZhtyWlHiyJmoFGrQWjxiT9")
            Debug.LogErrorEx("发送MD5请求-------------" .. Msg.s)
            Msg.c = tostring(sessionCode)
            if isCrossServer then
                Msg.isCrossServer = isCrossServer
            else
                Msg.isCrossServer = 0
            end
            -- Debug.LogErrorEx("发送登陆请求uid:" .. stringify(Msg))
            NetMgr:SendMsg(1001, Msg) --发送登陆消息
            local MainUserData = require("DynamicData.MainUserData").MainUserData
            MainUserData.uid = uid
        else
            -- function Recoonect()
            -- 	--self:CreateConnectAndLogin(jsondata)
            -- 	self:Login(uid, sessionCode, loginTime, self.chgIp, self.chgPort)
            -- end
            -- UIWinMgr:OpenNotice("无法连接服务器,是否重新连接", Recoonect)
            local LoginWintemp = UIWinMgr:GetWindow("LoginWin")
            LoginWintemp.LoadingMask.gameObject:SetActive(false)
            function Recoonect()
                --self:CreateConnectAndLogin(jsondata)
                UnityEngine.Application.Quit()
            end

            local str = UIWinMgr:GetIntTextByCode(49)
            UIWinMgr:OpenNotice(str, Recoonect)
            --UIWinMgr:OpenNotice("无法连接服务器是否退出游戏", Recoonect)
        end
    end

    NetMgr:CreateConnectionByAsync(serverIp, port, AsyncCallBack)
end

------新游戏切服
LoginModule.swServerDel = nil
function LoginModule:NewSwLogin(serverIp, port, myCallback)
    self.swServerDel = myCallback
    --uid
    local uid = UnityEngine.PlayerPrefs.GetString("LastAccount")
    --session
    local sessionCode = UnityEngine.PlayerPrefs.GetString("LastSec")
    local loginTime = os.time();
    Debug.log("loginTime:" .. loginTime);
    UnityEngine.PlayerPrefs.SetString("lastIp", serverIp) --缓存IP
    UnityEngine.PlayerPrefs.SetString("lastPort", port)   --缓存port
    self:Login(uid, sessionCode, loginTime, serverIp, port, 0)
end

function LoginModule:OpenLoginWin()
    UIWinMgr:OpenWindow("LoginWin")
end
